import requests
from flask import Blueprint,request,redirect,make_response,Response
from swaager_init import siwa,BaseModel
from config import VIP_EBD_TIME, LOGIN_APP_MAP
from application.models.model import *
from tools.broker_api import get_token_by_code
from flask_login import login_user
from tools.data_tools import ret_route

callback = siwa.blueprint("callback", __name__)
@callback.route("/<any(weibo,github,gitee):broker>", methods=['get'])
def github_cb(broker):
    code = request.args.get('code')
    token, uid = get_token_by_code(broker, code)
    broker_user = BrokerUser.query.filter_by(broker_uid=uid, broker_name=broker).first()
    if broker_user and broker_user.user:
        login_user(broker_user.user)
        return ret_route(2000)
    return ret_route(2000, data={'broker_id': uid, 'token': token, 'broker': broker})
